package com.flow.service.impl;

import java.util.List;

import com.flow.dao.IBankMessageDao;
import com.flow.model.BankMessage;
import com.flow.model.PagerModel;
import com.flow.service.IBankMessageService;
import com.flow.tools.StringTools;

public class BankMessageService<T> implements IBankMessageService<T> {

	private IBankMessageDao<T> bmDao;

	@Override
	public List<T> findAllBankMessage() {

		String hql = "from BankMessage";

		try {

			return bmDao.findBankMessageAllByHql(hql);

		} catch (Exception e) {

			e.printStackTrace();

		}

		return null;
	}

	@Override
	public PagerModel findAllBankMessageByPager() {

		PagerModel pager = new PagerModel();

		try {

			String hql = "from BankMessage";

			return bmDao.findBankMessagePagerModeled(pager, hql);

		} catch (Exception e) {

			e.printStackTrace();

		}

		return null;
	}

	@Override
	public boolean insertBankMessage(BankMessage bm) {

		if (null == bm)
			return false;

		if (!StringTools.nullOrEmpty(bm.getBmBankName()))
			return false;

		if(!StringTools.nullOrEmpty(bm.getBmNumber()))
			return false;
		
		if(!StringTools.nullOrEmpty(bm.getBmCardHolder()))
			return false;
		
		if (null == bm.getBmStatus())
			return false;

		if (bmDao.insertBankMessage(bm))
			return true;

		return false;
	}

	@Override
	public boolean deleteBankMessage(BankMessage bm) {

		if (null == bm)
			return false;

		if (null == bm.getBmId())
			return false;

		if (bmDao.deleteBankMessage(bm))
			return true;

		return false;
	}

	@Override
	public boolean updateBankMessage(BankMessage bm) {
		
		if (null == bm)
			return false;

		if (null == bm.getBmId())
			return false;

		if (!StringTools.nullOrEmpty(bm.getBmBankName()))
			return false;

		if(!StringTools.nullOrEmpty(bm.getBmNumber()))
			return false;
		
		if(!StringTools.nullOrEmpty(bm.getBmCardHolder()))
			return false;

		if (null == bm.getBmStatus())
			return false;

		if (bmDao.updateBankMessage(bm))
			return true;

		return false;
		
	}

	@Override
	public BankMessage findBankMessageById(String id) {
		
		if(!StringTools.nullOrEmpty(id))
			return null;
		
		try {
			
			String hql = "from BankMessage where bmId = ?";
			return bmDao.findBankMessageByHql(hql, Integer.valueOf(id));
			
		} catch (Exception e) {

		e.printStackTrace();
			
		}
		
		return null;
	}
	
	public void setBmDao(IBankMessageDao<T> bmDao) {
		this.bmDao = bmDao;
	}

	

}
